package com.it.bannner.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.haiheng.voiceandbook.BookRepository.Repository
import com.haiheng.voiceandbook.common.BaseDto
import com.haiheng.voiceandbook.common.DeviceInfoDto
import com.it.bannner.MyApplication
import com.it.bannner.common.DownRecor
import com.it.bannner.database.AppDatabase
import com.it.bannner.log.LogWriter
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 *
 */
class MyViewModel : ViewModel() {
    private val TAG = "MyViewModel"

    var videoList = MutableLiveData<BaseDto<MutableList<String>>>()
    var deviceInfoDto = MutableLiveData<BaseDto<DeviceInfoDto>>()

    val repository: Repository

    init {
        repository = Repository(
            AppDatabase.AppDataBaseSingleClass.getInstance(MyApplication.getInstance())!!
                .getDao()
        )
    }

    /**
     * 获取视频
     *
     */
    fun getVideo(type: Int, code: String) {
        viewModelScope.launch {
            try {
                val data = repository.getVideo(type, code)
                if (data == null) {
                    delay(5000)
                    LogWriter.e("getDeviceInfo: 5S之后重试")
                    getVideo(type, code)
                }
                videoList.value = data


            } catch (e: Exception) {
                LogWriter.e("getVideo: ${e}")
                delay(5000)
                getVideo(type, code)

            }
        }
    }

    /**
     * 获取设备信息
     *
     */
    fun getDeviceInfo(code: String, type: Int) {
        viewModelScope.launch {
            try {
                val data = repository.getDeviceInfo(code, type)
                deviceInfoDto.value = data
            } catch (e: Exception) {
                LogWriter.e("getDeviceInfo异常${e.message},20S之后重试 ${e}")
                delay(20000)
                getDeviceInfo(code, type)

            }
        }
    }


    suspend fun queryList(): List<DownRecor>? {
        return viewModelScope.async(Dispatchers.IO) {
            try {
                LogWriter.e("queryList")
                val result = repository.queryList()
                LogWriter.e("queryListByName结果: " + result)
                if (result.size <= 0) {
                    null
                } else {
                    result
                }
            } catch (e: Exception) {
                LogWriter.e("queryListByName异常: ${e}")
                null
            }
        }.await()

    }

    fun addRecord(recor: DownRecor) {
        viewModelScope.launch {
            try {
                val result = repository.addRecord(recor)
                LogWriter.e("addRecord: " + result)
            } catch (e: Exception) {
                LogWriter.e("addRecord: ${e}")
            }

        }
    }

    fun deleteRecord(recor: DownRecor) {
        viewModelScope.launch {
            try {
                LogWriter.e("deleteRecord参数: " + recor)
                val result = repository.deleteRecord(recor)
                LogWriter.e("deleteRecord结果: " + result)
            } catch (e: Exception) {
                LogWriter.e("deleteRecord异常: ${e}")
            }

        }
    }

    fun updateRecord(recor: DownRecor) {
        viewModelScope.launch {
            try {
                LogWriter.e("updateRecord参数: " + recor)
                val result = repository.updateRecord(recor)
                LogWriter.e("updateRecord结果: " + result)
            } catch (e: Exception) {
                LogWriter.e("updateRecord异常: ${e}")
            }

        }
    }

    suspend fun queryListByName(name: String): DownRecor? {
        return viewModelScope.async(Dispatchers.IO) {
            try {
                LogWriter.e("queryListByName参数: ${name}")
                val result = repository.queryListByName(name)
                LogWriter.e("queryListByName结果: " + result)
                result
            } catch (e: Exception) {
                LogWriter.e("queryListByName异常: ${e}")
                null
            }
        }.await()
    }

    suspend fun deleteAll() {
        viewModelScope.launch(Dispatchers.IO) {
            try {
                LogWriter.e("删除所有表的记录:")
                repository.deleteAll()
            } catch (e: Exception) {
                LogWriter.e("deleteAll异常: ${e}")
            }

        }
    }
}

